﻿using EFAPIProject.Services;
using Microsoft.AspNetCore.Mvc;

namespace EFAPIProject.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class UserController : ControllerBase
    {
        private readonly UserService _userService;

        public UserController(UserService userService)
        {
            _userService = userService;
        }

        // 新增用户
        [HttpPost]
        public async Task<IActionResult> CreateUser([FromBody] UserDto dto)
        {
            try
            {
                var userId = await _userService.CreateUserAsync(dto.UserName, dto.Email, dto.Age);
                return Ok(new { UserId = userId });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // 获取成年用户
        [HttpGet("adults")]
        public async Task<IActionResult> GetAdults()
        {
            var users = await _userService.GetAdultUsersAsync();
            return Ok(users);
        }
    }

    // 数据传输对象（DTO）
    public class UserDto
    {
        public string UserName { get; set; } = string.Empty;
        public string Email { get; set; } = string.Empty;
        public int? Age { get; set; }
    }
}
